﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AutomationMLMessage.cs" >
//   J. Prinz
// </copyright>
// <summary>
//   ViewModel for the Representation of AutomationML Messages, generated from the Validation Extension Methods
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AutomationMLMapper.ViewModels
{
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Windows.Data;

    /// <summary>
    ///  ViewModel for the Representation of AutomationML Messages, generated from the Validation Extension Methods of the AutomationML Engine
    /// </summary>
    public class AutomationMLMessage : CollectionViewSource, INotifyPropertyChanged
    {
        #region Fields

        /// <summary>
        /// Visibility of the Message-Container can be switched on and off.
        /// </summary>
        private bool showEngineMessageContainer;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="AutomationMLMessage"/> class. 
        /// </summary>
        public AutomationMLMessage()
        {
            this.Messages = new ObservableCollection<string>();
            this.Messages.CollectionChanged += this.MessagesCollectionChanged;
        }

        #endregion

        #region Public Events

        /// <summary>
        ///   Property Changed Notification
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Public Properties

        /// <summary>
        ///   List of Message-Strings
        /// </summary>
        public ObservableCollection<string> Messages
        {
            get
            {
                return this.Source as ObservableCollection<string>;
            }

            set
            {
                this.Source = value;
            }
        }

        /// <summary>
        ///   Visibility of the Message-Container can be switched on and off.
        /// </summary>
        public bool ShowEngineMessageContainer
        {
            get
            {
                return this.showEngineMessageContainer;
            }

            set
            {
                this.showEngineMessageContainer = value;
                this.OnNotifyPropertyChanged("ShowEngineMessageContainer");
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Source Changes Notification
        /// </summary>
        /// <param name="oldSource">
        /// Old Messages-Collection of the Message-Container
        /// </param>
        /// <param name="newSource">
        /// New Messages-Collection of the Message-Container
        /// </param>
        protected override void OnSourceChanged(object oldSource, object newSource)
        {
            base.OnSourceChanged(oldSource, newSource);
            var list = this.Source as ObservableCollection<string>;

            if (list != null)
            {
                this.ShowEngineMessageContainer = list.Count > 0;
            }

            this.OnNotifyPropertyChanged("View");
        }

        /// <summary>
        /// Handler for collection changed event of the Messages collection sets Visibility of the Container, if Items added
        /// </summary>
        /// <param name="sender">
        /// the sender
        /// </param>
        /// <param name="e">
        /// the event argument
        /// </param>
        private void MessagesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                this.ShowEngineMessageContainer = true;
            }
        }

        /// <summary>
        /// The on notify property changed.
        /// </summary>
        /// <param name="p">
        /// the Propertyname.
        /// </param>
        private void OnNotifyPropertyChanged(string p)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
        }

        #endregion
    }
}